package com.intouchtv.ari;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.AbsoluteLayout;

@SuppressWarnings("deprecation")
public class MovableAnimateImageView extends AnimatedImageView{

	private Rect primeryLocation;
	private Rect seconderyLocation;
	private Boolean isInPrimeryLocation; 
	private GestureDetector mGestureDetector;
	
	
	public MovableAnimateImageView(Context context, Bitmap[] images,Rect primeryLocation,
			  Rect seconderyLocation) {
		super(context, images);
		this.primeryLocation = primeryLocation;
		this.seconderyLocation = seconderyLocation;
		isInPrimeryLocation = true;
		
		 mGestureDetector = new GestureDetector(new LearnGestureListener());
	}
	
	
	public Boolean isInPrimeryLocation() {
		return isInPrimeryLocation;
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
	    if (mGestureDetector.onTouchEvent(event))
	        return true;
	    else
	        return false;
	}

	class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
	    @Override
	    public boolean onSingleTapUp(MotionEvent ev) {
	        return true;
	    }
	    @Override
	    public void onShowPress(MotionEvent ev) {
	    }
	    @Override
	    public void onLongPress(MotionEvent ev) {
	    }
	    @Override
	    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
	        return true;
	    }
	    @Override
	    public boolean onDown(MotionEvent ev) {
	        return true;
	    }
	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			if (isInPrimeryLocation)
			{	
				setLayoutParams(new AbsoluteLayout.LayoutParams(seconderyLocation.width(),seconderyLocation.height(),
						seconderyLocation.left,seconderyLocation.top));
				postInvalidate();
			}
			else {
				setLayoutParams(new AbsoluteLayout.LayoutParams(primeryLocation.width(),primeryLocation.height(),
						primeryLocation.left,primeryLocation.top));
				postInvalidate();
			}
			isInPrimeryLocation = !isInPrimeryLocation;
			

	        return true;
	    }
	}

}
